<?php
    get_header();
    global $wp_query,$wpdb;
    $current_url = explode('?', cpx_current_url());
    $url = $current_url[0];
    $parent = $wp_query->get_queried_object();
    $parent_name = $parent->name;
    $term_parent = $parent->parent;
    if($term_parent == 0){
        $parent_description = $parent->description;
    }else{
        $term = get_term( $term_parent, 'store_category' );
        $parent_description = $term->description;
    }
    $term_id_parent = $parent->term_id;
    $term_taxonomy_id_parent = $parent->term_taxonomy_id;
    $term_children = get_term_children( $term_id_parent, 'store_category' );
    $str_term_id_child ="";
    foreach ( $term_children as $child ) {
	$term = get_term_by( 'id', $child, 'store_category' );
        $str_term_id_child.=",$term->term_taxonomy_id";
    }
?>
<div class="row-fluid" id="category-wrap">
        <h1><?php echo $parent_name;?> Coupons & Promo Codes</h1>
        <div class="row-fluid wrap-des truncated">
            <p class="category-description"><?php echo $parent_description;?> <span class="less_desc">&lsaquo;&lsaquo;Less</span></p>
            <span class="more_desc">&rsaquo;&rsaquo;More</span>
        </div>
</div>
<?php
    $display = get_option('pagination_category');
    if($display == ''){
        $display = 15;
    }
    if(isset($_GET['show_page']) && (int)$_GET['show_page']){
        $page = $_GET['show_page'];
    }
            $my_query ="
                SELECT SQL_CALC_FOUND_ROWS {$wpdb->posts}.ID as cp_id FROM {$wpdb->posts}  
                INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)
                INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->postmeta}.meta_value= {$wpdb->term_relationships}.object_id) 
                WHERE 1=1 AND ( {$wpdb->term_relationships}.term_taxonomy_id IN ($term_taxonomy_id_parent $str_term_id_child) ) 
                AND {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id
                AND {$wpdb->posts}.post_type = 'coupon' AND ({$wpdb->posts}.post_status = 'publish') 
                AND {$wpdb->postmeta}.meta_key = 'store_coupon_metadata'
                GROUP BY {$wpdb->postmeta}.meta_value ORDER BY {$wpdb->posts}.post_date DESC
                ";
            $data = mysql_query($my_query);
            $record = mysql_num_rows($data);
            if($record > $display){
                $pages = ceil($record/$display);
            }else{
                $pages = 1;
            }
    $start = (isset($_GET['show_page']) && (int)$_GET['show_page'] >= 0) ? (($page - 1)*$display) : 0 ;
    $my_sql ="
        SELECT SQL_CALC_FOUND_ROWS {$wpdb->posts}.ID as cp_id FROM {$wpdb->posts}  
                INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)
                INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->postmeta}.meta_value= {$wpdb->term_relationships}.object_id) 
                WHERE 1=1 AND ( {$wpdb->term_relationships}.term_taxonomy_id IN ($term_taxonomy_id_parent $str_term_id_child) ) 
                AND {$wpdb->posts}.ID = {$wpdb->postmeta}.post_id
                AND {$wpdb->posts}.post_type = 'coupon' AND ({$wpdb->posts}.post_status = 'publish') 
                AND {$wpdb->postmeta}.meta_key = 'store_coupon_metadata'
                GROUP BY {$wpdb->postmeta}.meta_value ORDER BY {$wpdb->posts}.post_date DESC LIMIT $start,$display
    ";
    $result = $wpdb->get_results($my_sql);
    $total = count($result);
?>
<div class="row-fluid">
    <div class="span8 content-sidebar">
            <div class="row-fluid boxf tax-store-category">
                        <div class="row-fluid header-coupon">
                            <div class="span12 code-coupon"><h2>Current Top <?php echo $parent_name;?> Coupons</h2></div>
                        </div>
                        <?php
                            foreach ($result as $v){
                                $cp_id = $v->cp_id;
                                cpx_get_coupon_detail_2($cp_id);
                            }
                        ?>
                        <?php 
                            if($pages > 1 && $total > 0){
                                $current = ($start/$display)+1;
                                pagination_custom($url,$page,$pages,$current,3);
                            }
                        ?>
            <?php 
                if($total == 0){ ?>
                <div class="row-fluid notfound">
                    <span>The coupon you need is not available in this category.</span>
                    <p>But you should see these coupons below or go.</p>
                </div>
                <?php
                    $page = get_option('pagination_404');
                    cpx_get_coupon_detail_3('','',$page);
                }
                 ?>
       </div>
    </div>
    <div class="span4 right-sidebar">
        <div class="row-fluid topstoresxx">
            <div class="titlestopxx">Coupons From Same Category</div>
            <ul class="peoplewho">
            <?php 
                if($term_parent == 0){
                foreach ($term_children as $child){
                   $child_term = get_term_by( 'id', $child, 'store_category' ); ?>
                <li><a class="accessories" href="<?php echo get_term_link( $child_term->name, 'store_category');?>" title="<?php echo $child_term->name;?>"><?php echo $child_term->name;?></a></li>
            <?php }
                }else{
                    $term_child = get_term_children( $term_parent, 'store_category' );
                    foreach ($term_child as $child){
                        $child_term = get_term_by( 'id', $child, 'store_category' ); ?>
                        <li><a class="accessories" href="<?php echo get_term_link( $child_term->name, 'store_category');?>" title="<?php echo $child_term->name;?>"><?php echo $child_term->name;?></a></li>
            <?php }
                }
            ?>
            </ul>            
        </div>
         <?php if(is_active_sidebar('cs-main-sidebar')) dynamic_sidebar( 'cs-main-sidebar' ); ?>
    </div>
</div>
<?php get_footer(); ?>
